<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('example.view') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div class="mr10">
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form ref="formRef" :model="dataForm" label-width="100">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('example.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="4">
                  <el-image :src="dataForm.picture">
                    <template v-slot:error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </el-col>
                <el-col :span="20">
                  <el-descriptions border>
                    <!-- 第一行 -->
                    <el-descriptions-item width="200" :label="i18n('example.title')" label-align="right">
                      {{ dataForm.title }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('example.money')" label-align="right" :span="2">
                      {{ dataForm.money }}
                    </el-descriptions-item>

                    <!-- 第二行 -->
                    <el-descriptions-item width="200" :label="i18n('example.type')" label-align="right">
                      {{ dataForm.type }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('example.category_id')" label-align="right">
                      {{ dataForm.category_title }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('example.label.title')" label-align="right">
                      <el-tag class="mr5" v-for="item in dataForm.label" :key="item.id" effect="dark">
                        {{ item.title }}
                      </el-tag>
                    </el-descriptions-item>

                    <!-- 第三行 -->
                    <el-descriptions-item width="200" :label="i18n('example.time_length')" label-align="right">
                      {{ dataForm.statistical.example_time_length }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('example.member_total')" label-align="right">
                      {{ dataForm.statistical.member_total }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('example.organization_total')" label-align="right">
                      {{ dataForm.statistical.organization_total }}
                    </el-descriptions-item>

                    <!-- 第四行 -->
                    <el-descriptions-item width="200" :label="i18n('example.video_total')" label-align="right">
                      {{ dataForm.statistical.video_total }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('example.interaction_total')" label-align="right" :span="2">
                      {{ dataForm.statistical.interaction_total }}
                    </el-descriptions-item>

                    <!-- 第五行 -->
                    <el-descriptions-item width="200" :label="i18n('example.description')" label-align="right" :span="3">
                      {{ dataForm.description }}
                    </el-descriptions-item>

                    <!-- 第六行 -->
                    <el-descriptions-item width="200" :label="i18n('common.sort')" label-align="right" :span="3">
                      {{ dataForm.sort }}
                    </el-descriptions-item>

                    <!-- 第七行 -->
                    <el-descriptions-item width="200" :label="i18n('example.is_recommend')" label-align="right">
                      {{ dataForm.is_recommend }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('common.is_hidden')" label-align="right">
                      {{ dataForm.is_hidden }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('example.status')" label-align="right">
                      {{ dataForm.status }}
                    </el-descriptions-item>
                  </el-descriptions>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('example.detail_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <div v-html="dataForm.content"></div>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('example.resource_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20" class="mb20 text-center">
                <el-col :span="3" v-for="(item, key) in dataForm.resource" :key="key">
                  <el-card shadow="hover">
                    <div>
                      <el-image :src="item.url">
                        <template #error>
                          <div class="image-slot">
                            <el-icon :size="45"><Tickets /></el-icon>
                          </div>
                        </template>
                      </el-image>
                    </div>

                    <el-divider></el-divider>

                    <div>
                      <el-link :href="item.url" :underline="false" target="_blank">{{ item.filename }}</el-link>
                    </div>
                  </el-card>
                </el-col>
              </el-row>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="example-view">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/example')

  const dataForm = reactive({
    id: 0,
    category_title: '',
    label: [],
    picture: '',
    type: 1,
    title: '',
    description: '',
    content: '',
    money: 0.01,
    sort: 0,
    is_recommend: '',
    is_hidden: '',
    status: '',
    resource: [],
    statistical: []
  })

  const { formRef } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.category_title = data.data.category.title
            dataForm.picture = data.data.picture
            dataForm.type = data.data.type.text
            dataForm.title = data.data.title
            dataForm.description = data.data.description
            dataForm.money = data.data.money
            dataForm.sort = data.data.sort
            dataForm.is_recommend = data.data.is_recommend.text
            dataForm.is_hidden = data.data.is_hidden.text
            dataForm.status = data.data.status.text
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/detail/data`),
              method: 'get',
              params: $http.adornParams({
                'example_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                dataForm.content = data.data.content
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/label/select`),
              method: 'get',
              params: $http.adornParams({
                example_id: dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                data.data.forEach((res) => {
                  dataForm.label.push(res.label)
                })
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/statistical/data`),
              method: 'get',
              params: $http.adornParams({
                example_id: dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                dataForm.statistical = data.data
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/resource/select`),
              method: 'get',
              params: $http.adornParams({
                example_id: dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                dataForm.resource = data.data
              }
            })
          }
        })
      }
    })
  }

  onMounted(() => {
    init(formRef)
  })
</script>
